package cn.itcast.mybatis.dao;

import cn.itcast.mybatis.dao.impl.UserDaoImpl;
import cn.itcast.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/3.
 */
public class UserMapperTest {
    public UserDao userDao;

    @Before
    public void setUp() throws Exception {
        // 读取mybatis的全局配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 构建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取sqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 初始化userDao
        this.userDao = sqlSession.getMapper(UserDao.class);
    }

    @Test
    public void testQueryUserById() {
        System.out.println(this.userDao.queryUserById(1l));
    }

    @Test
    public void testQueryUserAll() {
        List<User> userList = this.userDao.queryUserAll();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testInsertUser() {
        User user = new User();
        user.setAge(16);
        user.setBirthday(new Date());
        user.setName("小红");
        user.setPassword("123456");
        user.setSex(2);
        user.setUserName("xiaohong");
        this.userDao.insertUser(user);
    }

    @Test
    public void testUpdateUser() {
        User user = userDao.queryUserById(7l);
        user.setName("秋水");
        user.setUserName("qiushui");
        user.setSex(1);
        this.userDao.updateUser(user);
    }

    @Test
    public void testDeleteUser() {
        this.userDao.deleteUser(8l);
    }

    @Test
    public void testLoginUser(){
       User user = this.userDao.loginUser("qiu");
        System.out.println(user);
    }
}
